package com.demo2do.fms.admin.web.controller.supplier;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.fms.admin.service.supplier.DriverService;
import com.demo2do.fms.admin.service.supplier.TractorService;
import com.demo2do.fms.admin.service.supplier.TrailerService;
import com.demo2do.fms.admin.service.supplier.VehicleSourceService;
import com.demo2do.fms.entity.driver.Driver;
import com.demo2do.fms.entity.driver.DriverAuditStatus;
import com.demo2do.fms.entity.vehicle.source.VehicleSource;
import com.demo2do.fms.entity.vehicle.tractor.Tractor;
import com.demo2do.fms.entity.vehicle.trailer.Trailer;

/**
 * 
 * @author dan
 *
 */
@Controller
@RequestMapping("/vehicle-sources")
@MenuSetting("menu-supplier")
public class VehicleSourceProfileEditController {
    
    @Autowired
    private VehicleSourceService vehicleSourceService;
    
    @Autowired
    private DriverService driverService;
    
    @Autowired
    private TractorService tractorService;
    
    @Autowired
    private TrailerService trailerService;
    
    /**
     * 
     * @param vehicleId
     * @param vehicleMenuType
     * @return
     */
    @RequestMapping(value = "/{vehicleId}", method = RequestMethod.GET)
    @MenuSetting("submenu-{vehicleMenuType}-vehicle-management")
    public ModelAndView view(@PathVariable("vehicleId") Long vehicleId,
                             @RequestParam("vehicleMenuType") String vehicleMenuType) {

        ModelAndView modelAndView = new ModelAndView("supplier/management/vehicle/profile/vehicle-profile-view");
        
        modelAndView.addObject("vehicleMenuType", vehicleMenuType);
        
        VehicleSource vehicleSource = vehicleSourceService.load(VehicleSource.class, vehicleId);
        modelAndView.addObject("vehicleSource", vehicleSource);
        
        // 运输公司 
        modelAndView.addObject("supplier", vehicleSource.getSupplier());
        
        // 司机车辆关联关系
        List<Driver> drivers = driverService.list(vehicleSource.getCode(), DriverAuditStatus.QUALIFIED);
        modelAndView.addObject("drivers", drivers);
        
        // 牵引车
        Tractor tractor = tractorService.load(Tractor.class, vehicleSource.getTractorId());
        modelAndView.addObject("tractor", tractor);
        
        // 挂车
        if(vehicleSource.hasTrailer()) {
            Trailer trailer = trailerService.load(Trailer.class, vehicleSource.getTrailerId());
            modelAndView.addObject("trailer", trailer);
        }

        return modelAndView;
        
    }

}